package com.star.book.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.star.book.entity.LoginInfo;
import com.star.book.entity.StarBook;
import com.star.book.entity.User;
import com.star.book.service.IBookInfoService;
import com.star.book.service.ILoginInfoService;
import com.star.book.service.IStarBookService;
import com.star.book.service.IUserService;
import com.star.book.utils.JudgeInternet;
import com.star.book.utils.SpringUtil;
import nl.bitwalker.useragentutils.Browser;
import nl.bitwalker.useragentutils.OperatingSystem;
import nl.bitwalker.useragentutils.UserAgent;
import org.openqa.selenium.remote.http.HttpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
/**
 * 后台登录页展示
 * */
@Controller
public class SysLoginController {

    @Autowired
    private IUserService userService;

    @Autowired
    private ILoginInfoService loginInfoService;

    @Autowired
    private  IStarBookService starBookService;

    @Autowired
    HttpServletRequest httpServletRequest;

    @GetMapping("/sys/login")
    public String login(HttpRequest request){
        return "system/login";
    }

    @PostMapping("/sys/login")
    public String login(@RequestParam String username, @RequestParam String password, Model m, HttpSession session){
        String ua = httpServletRequest.getHeader("User-Agent");
        //转成UserAgent对象
        UserAgent userAgent = UserAgent.parseUserAgentString(ua);
        //获取浏览器信息
        Browser browser = userAgent.getBrowser();
        //浏览器名称
        String browserName = browser.getName();

        try {
            QueryWrapper<User> qw = new QueryWrapper<User>();
            qw.eq("user_name",username);
            qw.eq("user_password",password);
            User one = userService.getOne(qw);
            LoginInfo loginInfo = SysLoginController.LoginInfo(username,browserName);
            if(one == null){
                loginInfo.setLoginStatus("登陆失败！");
                loginInfoService.save(loginInfo);
                return "system/login";
            }else{
                session.setAttribute("user",one);
                one.setUserStatus("在线");
                userService.update(one,qw);
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
                //设置最后登录时间
                one.setUserLastlogintime(df.format(new Date()));
                userService.update(one,qw);
                loginInfo.setLoginStatus("登陆成功！");
                loginInfoService.save(loginInfo);
                m.addAttribute("user",one);
                StarBook starLog = starBookService.selectStarBookByTime();
                m.addAttribute("log",starLog);
                List<User> users = userService.selectUserJiFen();
                m.addAttribute("users",users);
                return "system/index";
            }

        } catch (Exception e) {
            e.printStackTrace();
            return "system/login";
        }

    }
    /**
     * 登录日志信息记录
     * */
    public static LoginInfo LoginInfo(String username,String browserName) throws UnknownHostException {
        //登录日志记载
        LoginInfo loginInfo = new LoginInfo();
        InetAddress addr = InetAddress.getLocalHost();
        String ip=addr.getHostAddress();//获得本机IP
        String address=addr.getHostName();//获得本机名称
        String os = System.getProperty("os.name");//获取登录系统
        String userName = System.getProperty("user.name");//获取登录系统
        JudgeInternet internet = new JudgeInternet();
        boolean b = internet.internalIp(ip);
        if(b == true){
            loginInfo.setLoginInternet("内部IP");
        }else{
            loginInfo.setLoginInternet("公网IP");
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        loginInfo.setLoginTime(df.format(new Date()));
        loginInfo.setLoginHostname(address);
        loginInfo.setLoginIp(ip);
        loginInfo.setLoginOs(os);
        loginInfo.setLoginBrowser(browserName);
        loginInfo.setLoginOsuser(userName);
        loginInfo.setLoginName(username);
        return loginInfo;
    }


}
